package com.dfbz.demo;

import java.util.HashSet;
import java.util.Objects;
import java.util.Random;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo06_HashSet去重原理_02 {


    public static void main(String[] args) {

        Student s1 = new Student("小灰", 20);
        Student s2 = new Student("小灰", 20);

        HashSet<Student> set=new HashSet<>();

        set.add(s1);
        set.add(s2);

        System.out.println(set.size());         // 1
    }
}

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}